/*
 *  Transaction.h
 *  
 *
 *  Created by Shuqiao Guo on 12/21/11.
 *  Copyright 2011 Thothe. All rights reserved.
 *
 */

#ifndef _TRANSACTION_H_
#define _TRANSACTION_H_

#include <pthread.h>
#include <stack>
#include <map>

typedef enum MyObjType
{
    // database manager
    DB_MAN,
    
    // database instance
    DB_INS,
    
    // datum page
    DB_PAGE,
}ObjType;

typedef enum TransactionStatus
{
    //
    INITIALIZED,
    //
    BEGIN,
    //
    RUNNING,
    //
    COMMITTED,
    //
    ABORTED
}TransStatus;

typedef struct LockEntryStr
{
    ObjType             type;
    pthread_mutex_t    *objLock;
    void*               originalObj;
    void*               copyObj;
}LockEntry;

using namespace std;

class Transaction
{
public:
	Transaction();
	~Transaction();

    bool AddDatumPage(pthread_mutex_t* l, void* originalPage, void* copyPage);
    
    bool AddDatabase(pthread_mutex_t* l, void* originalDB, void* copyDB);

    bool AddDBManager(pthread_mutex_t* l, void* orgDBMan, void* copyDBMan);

    bool Commit();
    
    bool Abort();
    
    bool AbortLast(void* pt);

    void* FindLink(void* pt);
    
private:
	stack<LockEntry> *lockStack;
    map<void*,void*> *lockMap;
    
    TransStatus status;
    
};

#endif
